<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>商品选购</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .cart-link {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            text-decoration: none;
            border-radius: 4px;
        }
        .products {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
        }
        .product-card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            overflow: hidden;
            transition: transform 0.3s;
        }
        .product-card:hover {
            transform: translateY(-5px);
        }
        .product-image {
            width: 100%;
            height: 180px;
            object-fit: cover;
        }
        .product-info {
            padding: 15px;
        }
        .product-title {
            font-size: 18px;
            margin: 0 0 10px 0;
            color: #333;
        }
        .product-price {
            font-size: 20px;
            font-weight: bold;
            color: #e53935;
            margin-bottom: 15px;
        }
        .add-to-cart {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .add-to-cart:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>商品选购</h1>
        <a href="cart.jsp" class="cart-link">查看购物车 (<%= session.getAttribute("cart") != null ? ((Map<Integer,Integer>)session.getAttribute("cart")).size() : 0 %>)</a>
    </div>

    <div class="products">
        <%
            // 模拟商品数据库
            List<Map<String, Object>> products = new ArrayList<>();

            Map<String, Object> product1 = new HashMap<>();
            product1.put("id", 1);
            product1.put("name", "智能手机");
            product1.put("price", 2999.00);
            product1.put("image", "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1730124020.8676597.png");
            products.add(product1);

            Map<String, Object> product2 = new HashMap<>();
            product2.put("id", 2);
            product2.put("name", "笔记本电脑");
            product2.put("price", 5999.00);
            product2.put("image", "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1740627851.66339024.jpg");
            products.add(product2);

            Map<String, Object> product3 = new HashMap<>();
            product3.put("id", 3);
            product3.put("name", "无线耳机");
            product3.put("price", 399.00);
            product3.put("image", "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1740638541.0557913.png");
            products.add(product3);

            Map<String, Object> product4 = new HashMap<>();
            product4.put("id", 4);
            product4.put("name", "智能手表");
            product4.put("price", 1299.00);
            product4.put("image", "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1721233615.7826806.png");
            products.add(product4);

            for(Map<String, Object> product : products) {
        %>
        <div class="product-card">
            <img src="<%= product.get("image") %>" alt="<%= product.get("name") %>" class="product-image">
            <div class="product-info">
                <h3 class="product-title"><%= product.get("name") %></h3>
                <div class="product-price">¥<%= String.format("%.2f", product.get("price")) %></div>
                <form action="addToCart.jsp" method="post">
                    <input type="hidden" name="productId" value="<%= product.get("id") %>">
                    <input type="hidden" name="productName" value="<%= product.get("name") %>">
                    <input type="hidden" name="productPrice" value="<%= product.get("price") %>">
                    <button type="submit" class="add-to-cart">加入购物车</button>
                </form>
            </div>
        </div>
        <% } %>
    </div>
</div>
</body>
</html>